<?php
namespace app\common\model\paths;

use think\admin\Model;

/**
 * 用户班级表
 * @package app\common\model\paths
 * @Author Lihongqi
 * @DateTime 2024-09-05 17:01:53
 */
class UserClassModel extends Model
{
    const  STATUS_APPLY = 0; // 待审核
    const  STATUS_AGREE = 1; // 同意
    const  STATUS_REFUSE = 2 ; // 拒绝

// 设置当前模型对应的完整数据表名称
    protected $name = 'paths_user_class';

    // 自动写入时间戳
    protected $autoWriteTimestamp = true;


    /**
     * 获得状态
     *
     * @return string[]
     */
    public static function getStatus()
    {
        return [
            self::STATUS_APPLY => lang('待审核'),
            self::STATUS_AGREE => lang('同意'),
            self::STATUS_REFUSE => lang('拒绝'),
            ];
    }

    /**
     * 校验用户是否已加入班级
     * @author wangqinghui
     * @DateTime 2024-09-13 19:37:40
     * @param integer $user_id
     * @param integer $task_id
     * @return void
     */
    public function checkUserClass(int $user_id, int $task_id)
    {
        $ret = ['code' => 0, 'msg' => '', 'data' => []];
        if(empty($user_id) || ($user_id <= 0)){
            $ret['msg'] = '请登录用户';
            $ret['code'] = -1200;
            $ret['data'] = sysuri('login/index');
            return $ret;
        }

        if(empty($task_id) || ($task_id <= 0)){
            $ret['msg'] = '课程信息有误';
            return $ret;
        }

        // 获取课程所属班级
        $class_data = ClassTaskModel::mk()->where(['task_id' => $task_id])->column('class_id');
        if(empty($class_data)){
            $ret['msg'] = '班级信息有误';
            return $ret;
        }

        $map = [
            ['index_user_id', '=', $user_id],
            ['class_id', 'in', $class_data],
            ['status', '=', self::STATUS_AGREE]
        ];
        $count = self::mk()->where($map)->count();
        if($count <= 0){
            $ret['msg'] = '请先加入班级';
            return $ret;
        }
        $ret = ['code' => 1, 'msg' => '通过', 'data' => []];
        return $ret;
    }

    /**
     * 获取用户加入该课程下的班级
     * @author wangqinghui
     * @DateTime 2024-09-30 16:25:32
     * @param integer $user_id
     * @param integer $task_id
     * @return array
     */
    public function getMyTaskClasses(int $user_id, int $task_id): array
    {
        $ret = [[],[]];
        if(empty($user_id) || ($user_id <= 0)){
            return $ret;
        }

        if(empty($task_id) || ($task_id <= 0)){
            return $ret;
        }

        // 获取课程所属班级
        $class_data = ClassTaskModel::mk()->where(['task_id' => $task_id])->column('class_id,platform_id,user_id', 'class_id');
        if(empty($class_data)){
            return $ret;
        }
        $class_ids = array_keys($class_data);

        $map = [
            ['index_user_id', '=', $user_id],
            ['class_id', 'in', $class_ids],
            ['status', '=', self::STATUS_AGREE]
        ];
        $user_class_ids = self::mk()->where($map)->column('class_id');
        $ret = [$user_class_ids, $class_data];
        return $ret;
    }
}